const path = require('path')
console.log('path_____________________', path)
// 初始化项目
const targetUrl = 'http://172.16.0.229:8887' //HT开发地址
const targetName = 'admin_server'
var initialEnv = targetUrl.replace(/\//g, '_')
var env = initialEnv.replace(/:/g, '#')
const TimeStamp = new Date().getTime()

function resolve(dir) {
  return path.join(__dirname, dir)
}

//添加插件compression-webpack-plugin 为打包文件中js文件生成对应压缩文件，加快页面加载速度
const webpack = require('webpack')
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const productionGzipExtensions = ['js', 'css']

// vue.config.js
module.exports = {
  // 如果你不需要生产环境的 source map，可以将其设置为 false 以加速生产环境构建。
  productionSourceMap: false,
  publicPath: '/', // 设置打包文件相对路径
  outputDir: targetName + env,
  configureWebpack: {
    output: {
      // 输出重构  打包编译后的 文件名称  【模块名称.时间戳.js】
      filename: `js/[name].${TimeStamp}.js`,
      chunkFilename: `js/[name].${TimeStamp}.js`,
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
        '@i': path.resolve(__dirname, './src/assets'),
      },
    },
    // 这里是配置 开发的时候注释掉 防止编译添加解压包
    plugins: [
      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
      // 下面是下载的插件的配置
      new CompressionWebpackPlugin({
        algorithm: 'gzip',
        test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
        threshold: 10240,
        minRatio: 0.8,
      }),
      new webpack.optimize.LimitChunkCountPlugin({
        maxChunks: 5,
        minChunkSize: 100,
      }),
    ],
  },

  /*
  pages: {
    index: {
      entry: 'src/main.js',
      chunks: ['chunk-vendors', 'chunk-common', 'index']
    }
  },
  */
  chainWebpack: (config) => {
    config.resolve.alias
      .set('@$', resolve('src'))
      .set('@api', resolve('src/api'))
      .set('@assets', resolve('src/assets'))
      .set('@comp', resolve('src/components'))
      .set('@views', resolve('src/views'))
      .set('@layout', resolve('src/layout'))
      .set('@static', resolve('src/static'))
  },

  css: {
    loaderOptions: {
      less: {
        modifyVars: {
          /* less 变量覆盖，用于自定义 ant design 主题 */
          /*
          'primary-color': '#F5222D',
          'link-color': '#F5222D',
          'border-radius-base': '4px',
          */
        },
        javascriptEnabled: true,
      },
    },
  },

  devServer: {
    disableHostCheck: false,
    port: 3000,
    proxy: {
      '/admin_server': {
        target: targetUrl,
        ws: false,
        changeOrigin: true,
      },
    },
  },
  lintOnSave: undefined,
  runtimeCompiler: true,
}
